<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Anote;
use App\Models\Amodule;
use App\Models\Areply;
class IndexController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    //前台首页
    public function getIndex(){
        $data = anote::with('huser','amodule')->where('state','=','2')->paginate(5);
        return view('home.index.index',['data'=>$data]);
    }
    //帖子详情
    public function getContent($id){

        $data = anote::with('huser','amodule')->find($id);
        $data->increment('click');
        $areply = new Areply();
        $areply = $areply->where('nid',$id)->orderBy('rtime')->get();
        // dd($areply);

        $datas = anote::where('state','=','2')->paginate(4);
        // dd($data);
        return view('home.content.index',['data'=>$data,'datas'=>$datas,'areply'=>$areply]);
    }
    //社区
    public function getAmodule(Request $request)
    {
        $Amodule = new Amodule();
        $datas = $Amodule->tree();
        $a = $request->input();
        // $a = $a?$a:'desc';
        //轮播图
        $carousel = anote::with('amodule')->orderBy('ntime')->where('pic','like','%/ueditor/php/upload/image/%')->limit(3)->get();
        //轮播图旁边列表
        $datae = anote::with('huser','amodule')->orderBy('ntime')->limit(9)->get();
        // dump($a);
        // 文章列表
        $data = anote::with('huser','amodule')->orderBy('ntime')->paginate(6);
        //文章推荐
        $dataa = anote::where('state','=','2')->limit(3)->get();
        // dd($dataa);
        // dd($data[0]->huser);
        return view('home.amodule.index',['data'=>$data,'datas'=>$datas,'carousel'=>$carousel,'datae'=>$datae,'dataa'=>$dataa]);
    }
    // public function getAjax(Request $request){
    //     echo 1;
    // }
    // 发帖帖子视图
        public function getAnote(Request $request)
    {
        $Amodule = new Amodule();
        $data = $Amodule->tree();
        return view('home.anote.add',['data'=>$data]);
    }

    public function postAnoteadd(Request $request){
        // dd($request->input());
        $Anote = new Anote;
        $data = $request->except('_token');
        $parrent = "/(href|src)=([\"|']?)([^\"'>]+.(jpg|JPG|jpeg|JPEG|gif|GIF|png|PNG))/i";
        $a = $data['content'];
        preg_match($parrent,$a,$match);
        $match = $match[3];
        $Anote->title = $data['title'];
        $Anote->classify = $data['classify'];
        $Anote->content = $data['content'];
        $Anote->pic = $match;
        $data = $Anote->save();
        if($data){
            return redirect('/');
        }else{
            return back()->with('msg','修改失败');
        }
    }

    // public function 


}
